﻿using DavidCms.Web.Models;
using MvcSiteMapProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPager;

namespace DavidCms.Web.Controllers
{
    public class CmsController : BaseController
    {
        CmsContext db = new CmsContext();
        public ActionResult Index()
        {
            var IndexSlides = from p in db.Image
                              where p.Category == "IndexSlides"
                              select p;
            ViewBag.IndexSlides = IndexSlides.ToList();
            return View();
        }

        public ActionResult List(string Para, int PageIndex = 1)
        {
            var navigation = from p in db.Navigation
                             where p.EnglishName == Para
                             select p;
            ViewBag.Title = navigation.FirstOrDefault().Name;
            string Url = Request.Url.Authority;
            var query = from q in db.Article
                        join p in db.Navigation on q.NavigationId equals p.Id
                        where p.EnglishName == Para
                        select q;
            List<ArticleListVO> articleList = new List<ArticleListVO>();
            foreach (var q in query.ToList())
            {
                articleList.Add(new ArticleListVO
                        {
                            Id = q.Id,
                            Title = q.Title,
                            Editer = q.Editer,
                            Description = q.Description,
                            PublishTime = q.publishTime,
                            NavigationEnglishName = q.NavigationEnglishName,
                        });
            }
            var model = articleList.ToPagedList(PageIndex, 2);
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ArticleList", model);
            }
            return View(model);
        }

        public ActionResult NavDetail(string Para)
        {
            var query = from q in db.Article
                        join p in db.Navigation on q.NavigationId equals p.Id
                        where p.EnglishName == Para
                        select new ArticleDetailVO
                        {
                            Title = q.Title,
                            Editer = q.Editer,
                            Content = q.Content,
                            PublishTime = q.publishTime,
                        };
            return View(query.FirstOrDefault());
        }

        //[MvcSiteMapNodeAttribute(Title = "详细信息", ParentKey = "SuccessfulCase", DynamicNodeProvider = "DavidCms.Web.SuccessfulCaseListDetailDynamicNodeProvider,DavidCms")]
        public ActionResult ListDetail(int Id, string Para)
        {
            //int articleId=int.Parse(Para);
            var query = from q in db.Article
                        where q.Id == Id
                        select new ArticleDetailVO
                        {
                            Title = q.Title,
                            Editer = q.Editer,
                            Content = q.Content,
                            PublishTime = q.publishTime,
                        };
            return View(query.FirstOrDefault());
        }

        public PartialViewResult Menu()
        {
            ViewBag.CurrentPath = Request.Url.AbsolutePath;
            ViewBag.Navs = this.NavVoList;
            return PartialView("_MenuPartial");
        }

        public PartialViewResult CompanyNotice()
        {
            ViewBag.CompanyNotice = "今天中秋节放假！每个员工方法一盒月饼，祝大家中秋节快乐。";
            return PartialView("_CompanyNoticePartial");
        }

        public ActionResult Search(string KeyWord, int PageIndex = 1)
        {
            var query = from q in db.Article
                        where q.Title.Contains(KeyWord)
                        select q;
            List<ArticleListVO> articleList = new List<ArticleListVO>();
            foreach (var q in query.OrderByDescending(p => p.publishTime).ToList())
            {
                articleList.Add(new ArticleListVO
                {
                    Id = q.Id,
                    Title = q.Title,
                    Editer = q.Editer,
                    Description = q.Description,
                    PublishTime = q.publishTime,
                    NavigationEnglishName = q.NavigationEnglishName,
                });
            }
            ViewBag.Title = "站内搜索";
            var model = articleList.ToPagedList(PageIndex, 2);
            if(Request.IsAjaxRequest())
            {
                return PartialView("_ArticleList",model);
            }
            return View("List", model);
        }

    }
}
